package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.PermissionService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.po.Permission;
import com.huilian.iotbox.data.vo.RoleVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 少凡
 * @date 2020/2/26 15:40
 */
@RestController
@RequestMapping("/api/permission")
public class PermissionController extends BaseController {

    @Resource
    private PermissionService permissionService;

    @Resource
    private UserService userService;


    /**
     * 根据角色获取菜单
     *
     * @return
     */
    @GetMapping("/menus")
    public ServerResponse getMenus(
            HttpServletRequest request,
            @RequestParam(required = false) Boolean nav,
            String permissionId,
            @RequestParam(required = true) Integer platformType
    ) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        List<Integer> roleIdList = new ArrayList<>();
        for (RoleVo rv : userInfo.getRoles()) {
            roleIdList.add(rv.getRoleId());
        }

        return permissionService.getMenus(roleIdList, nav, permissionId, userInfo, platformType);
    }

    /**
     * 根据角色ID找出所有的子权限，菜单和根限
     *
     * @return
     */
    @GetMapping("/list")
    public ServerResponse getListByRoleId(
            HttpServletRequest request,
            @RequestParam(required = false) Boolean noAuth,
            @RequestParam(required = true) Integer platformType
    ) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        List<Integer> roleIdList = new ArrayList<>();
        for (RoleVo rv : userInfo.getRoles()) {
            roleIdList.add(rv.getRoleId());
        }
        return permissionService.getListByRoleId(roleIdList, noAuth, platformType);
    }


    /**
     * 添加权限
     *
     * @param permission
     * @return
     */
    @PostMapping
    public ServerResponse add(@RequestBody Permission permission, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return permissionService.add(permission, userInfo);
    }

    /**
     * 修改权限
     *
     * @param permission
     * @return
     */
    @PutMapping
    public ServerResponse update(@RequestBody Permission permission, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return permissionService.update(permission, userInfo);
    }

    /**
     * 删除权限
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public ServerResponse delete(@PathVariable Integer id, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return permissionService.delete(id, userInfo);
    }
}
